﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PI_CS_Library.PicsNetwork_Information_Model;
using PI_CS_Library;

namespace PI_CS_GUI
{
    public partial class DelLinkGUI : Form
    {
        PicsNetworkController _nc;
        GUIDelegate _gui;

        public DelLinkGUI(PicsNetworkController _nc, GUIDelegate _gui)
        {
            this._nc = _nc;
            this._gui = _gui;
            InitializeComponent();
        }

        private void DelLinkGUI_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            _gui(this, new GUIArgs("delLinkCancel"));
            this.Close();
        }

        private void DelLinkGUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            _gui(this, new GUIArgs("delLinkCancel"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int _linkId;
            string _srcName;
            string _dstName;

            if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "")
                MessageBox.Show("Link not specified");
            else if (textBox3.Enabled == true)
            {
                _linkId = Int32.Parse(textBox3.Text);
                if (_nc.Network.LinkResourceList.ContainsKey(_linkId))
                {
                    _nc.Network.RemoveLink(_linkId, _nc, _gui, true, -1);
                    _srcName = _nc.Network.LinkResourceList[_linkId].SrcMatrix.Name;
                    _dstName = _nc.Network.LinkResourceList[_linkId].SinkMatrix.Name;

                    if (checkBox1.Checked)
                    {
                        _linkId = _nc.Network.GetLinkId(_dstName, _srcName);
                        if (_linkId != -1)
                            _nc.Network.RemoveLink(_linkId, _nc, _gui, true, -1);

                        else
                            MessageBox.Show("Opposite link doesn't exist");
                    }

                    this.Close();
                }
                else
                    MessageBox.Show("Link with specified ID doesn't exist");
            }
            else
            {
                _srcName = textBox1.Text;
                _dstName = textBox2.Text;

                _linkId = _nc.Network.GetLinkId(_srcName, _dstName);

                if (_linkId != -1)
                {
                    _nc.Network.RemoveLink(_linkId, _nc, _gui, true, -1);

                    if (checkBox1.Checked)
                    {
                        _linkId = _nc.Network.GetLinkId(_dstName, _srcName);
                        if (_linkId != -1)
                            _nc.Network.RemoveLink(_linkId, _nc, _gui, true, -1);
                        else
                            MessageBox.Show("Opposite link doesn't exist");
                    }

                    this.Close();
                }
                else MessageBox.Show("Link between these matrixes dosesn't exist");
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                textBox3.Enabled = false;
            else
                if (textBox2.Text == "")
                    textBox3.Enabled = true;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
                textBox3.Enabled = false;
            else
                if (textBox1.Text == "")
                    textBox3.Enabled = true;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
            else
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
        }
    }
}
